let app = getApp()
import detailCanvas from '../../../utils/extension.js';
Component({
    properties: {
          type:String
      },
      data:{
        type: "",
        savePosterUrl: "",
        saveText:"保存到相册，分享给好友",
        qrcodeUrl: ""
      },
    lifetimes: {
        attached: function () {
          if(this.data.type=='extension'){
            wx.setNavigationBarTitle({
              title:'推广全店'
            })
          }else{
            wx.setNavigationBarTitle({
              title:'邀请客户'
            })
          }
          wx.getSystemInfo({
            success: (res)=>{
              this.setData({
                screen_width: res.windowWidth/375,
                width:res.windowWidth,
                screen_height: res.windowHeight
             })
            },
          })
          this.getQrCod()
          this.setData({
            getGlobalColor:app.globalData.globalColor
          })
       }
      },
    methods: {
      saveShareImg: function () {
        let posters = this.data.posters;
        wx.saveImageToPhotosAlbum({
         filePath: posters,
         success: (res)=>{
           wx.showToast({
             title:'保存成功',
             icon:'success'
           })
         },
         fail: (err)=>{
           wx.showToast({
             title: '请授权保存图片',
             icon: 'none',
           });
           this.setData({
             isShare:true
           })
         },
       })
       },
      getCode(img){
        return new Promise((resolve,reject)=>{
          wx.downloadFile({
            url: img,
            success: (res) => {
              resolve(res)
            }
          })
        })
      },
      getQrCod:function(){
        wx.showLoading({
          title: '请稍后',
          mask: true,
        });
          var that=this
          wx.request({ 
            url: app.globalData.siteroot,
            data: {
              do:'Qrcode_getQrCode',
              path:'yb_shopv2/entry/video-market/index?pid='+wx.getStorageSync('distribution_id')+'&sid='+wx.getStorageSync('staff_bind_id'),
            },
            success: function(res) {
              that.setData({
                codeurl:res.data.qrurl
               })
               let title =app.globalData.userInfo.nickName;
               let codeImg =res.data.qrurl;
               let headUrl = app.globalData.siteDomain+'/addons/yb_shopv2_video/core/head_convert.php?url='+wx.getStorageSync('userinfo').user_headimg;
               Promise.all([
                that.getCode(codeImg),
                that.getCode(headUrl),
                that.getCode(wx.getStorageSync('distribution').share_pic)
               ]).then(res=>{
                   let sWidth = that.data.screen_width;
                   let sHeight = that.data.screen_height;
                   let code = res[0].tempFilePath;
                   let headimg = res[1].tempFilePath;
                   let share_pic= res[2].tempFilePath;
                   detailCanvas(that,share_pic,sWidth,sHeight,headimg,code,title,function(res){
                   wx.hideLoading();
                   that.setData({
                     posters:res
                   });
                 });
               })
            }
          })
      }
    }
});
